/**
 * 学生考勤系统的动态模拟数据生成工具
 */

// 学生名字池
const firstNames = ['张', '李', '王', '赵', '陈', '刘', '杨', '黄', '周', '吴', '郑', '孙', '马', '朱', '胡', '林', '郭', '何', '高', '罗'];
const lastNames = ['伟', '芳', '娜', '敏', '静', '强', '磊', '洋', '艳', '勇', '军', '杰', '娟', '涛', '超', '明', '霞', '平', '刚', '健'];

// 班级名字
const classNames = ['一年级（1）班', '一年级（2）班', '二年级（1）班', '二年级（2）班', '三年级（1）班', '三年级（2）班'];

// 缺勤原因池
const absenceReasons = ['感冒', '发烧', '家中有事', '外出就医', '身体不适', '参加比赛', '探亲', '家长临时接走', '心理疏导', '其他原因'];

/**
 * 生成随机数字（包含最小值和最大值）
 */
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**
 * 随机生成学生姓名
 */
function generateRandomName() {
  const firstName = firstNames[getRandomInt(0, firstNames.length - 1)];
  const lastName = lastNames[getRandomInt(0, lastNames.length - 1)];
  return firstName + lastName;
}

/**
 * 生成随机学号 (6-8位数字)
 */
function generateStudentId() {
  const year = getRandomInt(2020, 2025).toString();
  const classNum = getRandomInt(1, 12).toString().padStart(2, '0');
  const sequence = getRandomInt(1, 99).toString().padStart(2, '0');
  return year + classNum + sequence;
}

/**
 * 生成随机日期 (YYYY-MM-DD 格式)
 */
function generateRandomDate(startDate, endDate) {
  const start = startDate ? new Date(startDate).getTime() : new Date('2024-01-01').getTime();
  const end = endDate ? new Date(endDate).getTime() : new Date('2025-12-31').getTime();
  
  const randomTimestamp = getRandomInt(start, end);
  const date = new Date(randomTimestamp);
  
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  
  return `${year}-${month}-${day}`;
}

/**
 * 生成随机时间 (HH:MM 格式)
 */
function generateRandomTime() {
  const hour = String(getRandomInt(7, 17)).padStart(2, '0');
  const minute = String(getRandomInt(0, 59)).padStart(2, '0');
  return `${hour}:${minute}`;
}

/**
 * 生成随机日期时间 (YYYY-MM-DD HH:MM 格式)
 */
function generateRandomDateTime(startDate, endDate) {
  return `${generateRandomDate(startDate, endDate)} ${generateRandomTime()}`;
}

/**
 * 生成考勤记录
 */
function generateAttendanceRecord(id, className, timePeriod, date) {
  const total = getRandomInt(40, 55);
  const absent = getRandomInt(0, Math.floor(total * 0.1)); // 缺勤人数不超过总人数的10%
  const present = total - absent;
  const attendanceRate = ((present / total) * 100).toFixed(1) + '%';
  
  return {
    id,
    timePeriod,
    className,
    date: date || generateRandomDate('2025-03-01', '2025-03-31'),
    total,
    present,
    absent,
    attendanceRate
  };
}

/**
 * 生成详细的学生考勤列表
 */
function generateStudentAttendanceDetails(totalStudents, absentCount) {
  const students = [];
  
  // 生成所有学生
  for (let i = 0; i < totalStudents; i++) {
    const studentName = generateRandomName();
    const studentId = generateStudentId();
    
    students.push({
      studentName,
      studentId,
      status: '正常',
      reason: ''
    });
  }
  
  // 随机选择部分学生为缺勤
  const indices = new Set();
  while (indices.size < absentCount) {
    indices.add(getRandomInt(0, totalStudents - 1));
  }
  
  // 标记缺勤学生
  for (const index of indices) {
    students[index].status = '缺勤';
    students[index].reason = absenceReasons[getRandomInt(0, absenceReasons.length - 1)];
  }
  
  return students;
}

/**
 * 生成请假记录
 */
function generateLeaveRecord(id, className, studentName, studentId) {
  const leaveTypes = ['病假', '事假'];
  const statuses = ['待审批', '已批准', '已拒绝'];
  const approvers = ['王老师', '李老师', '张老师', '刘老师'];
  
  const startDate = generateRandomDate('2025-03-01', '2025-03-25');
  const endDateObj = new Date(startDate);
  endDateObj.setDate(endDateObj.getDate() + getRandomInt(1, 5));
  const endDate = formatDate(endDateObj);
  
  const leaveType = leaveTypes[getRandomInt(0, leaveTypes.length - 1)];
  const status = statuses[getRandomInt(0, statuses.length - 1)];
  const reason = leaveType === '病假' 
    ? `${absenceReasons[getRandomInt(0, 4)]}，需要休息`
    : `${absenceReasons[getRandomInt(5, absenceReasons.length - 1)]}`;
  
  return {
    id,
    studentName: studentName || generateRandomName(),
    studentId: studentId || generateStudentId(),
    className,
    leaveType,
    startDate,
    endDate,
    status,
    reason,
    approver: status === '待审批' ? '' : approvers[getRandomInt(0, approvers.length - 1)]
  };
}

/**
 * 生成离校记录
 */
function generateLeaveSchoolRecord(id, className, studentName, studentId) {
  const statuses = ['待审批', '已离校', '已返校'];
  const escorts = ['父亲', '母亲', '爷爷', '奶奶', '姑姑', '叔叔'];
  
  const date = generateRandomDate('2025-03-01', '2025-03-31');
  const leaveTime = `${date} ${generateRandomTime()}`;
  
  // 返校时间在离校时间后1-3小时
  const returnTimeObj = new Date(leaveTime);
  returnTimeObj.setHours(returnTimeObj.getHours() + getRandomInt(1, 3));
  
  const returnYear = returnTimeObj.getFullYear();
  const returnMonth = String(returnTimeObj.getMonth() + 1).padStart(2, '0');
  const returnDay = String(returnTimeObj.getDate()).padStart(2, '0');
  const returnHour = String(returnTimeObj.getHours()).padStart(2, '0');
  const returnMinute = String(returnTimeObj.getMinutes()).padStart(2, '0');
  
  const returnTime = `${returnYear}-${returnMonth}-${returnDay} ${returnHour}:${returnMinute}`;
  
  const status = statuses[getRandomInt(0, statuses.length - 1)];
  
  return {
    id,
    studentName: studentName || generateRandomName(),
    studentId: studentId || generateStudentId(),
    className,
    leaveTime,
    returnTime,
    status,
    reason: absenceReasons[getRandomInt(0, absenceReasons.length - 1)],
    escort: escorts[getRandomInt(0, escorts.length - 1)]
  };
}

/**
 * 日期对象格式化为 YYYY-MM-DD 字符串
 */
function formatDate(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  
  return `${year}-${month}-${day}`;
}

export {
  classNames,
  generateRandomName,
  generateStudentId,
  generateRandomDate,
  generateRandomTime,
  generateRandomDateTime,
  generateAttendanceRecord,
  generateStudentAttendanceDetails,
  generateLeaveRecord,
  generateLeaveSchoolRecord
}; 